<template>
  <div style="width: 100%">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="form.nickname"></el-input>
      </el-form-item>
      <el-form-item label="部门" prop="deptId">
        <el-select v-model="form.deptId" @change="selectDept">
          <el-option v-for="item in deptList" v-bind:key="item.deptId" :value="item.deptId" :label="item.name" v-text="item.name"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="form.mobile"></el-input>
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input disabled v-model="form.email"></el-input>
      </el-form-item>
      <el-form-item label-width="0px" style="text-align: center">
        <el-button type="primary" @click="onSubmit">{{form.userId?'保存修改':'确认添加'}}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "edit-people",
  props: {
    deptList: {
      type: Array
    }
  },
  data() {
    return {
      form: {},
      rules: {
        nickname: [
          {required: true, message: '请输入姓名', trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    onSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.$emit('submit',this.form)
        } else {
          return false;
        }
      })
    },
    reset() {
      this.form = {}
    },
    loadData(form) {
      this.form = form
    },
    selectDept(obj) {
      let that = this
      this.deptList.forEach(function (item) {
        if(item.deptId == obj) {
          that.form.deptName = item.name
          return
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
